﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Notification;
using System.Collections.ObjectModel;
using System.Globalization;
using Microsoft.Advertising.Mobile.UI;
using Microsoft.Phone.Scheduler;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Net.NetworkInformation;
using System.Threading;
using OnlineVideos.Library;
using Common.Library;
using OnlineVideos.Common;
using OnlineVideos.UI;
using Common.Utilities;
using System.IO;
using System.Device.Location;
using Microsoft.Phone.Tasks;
using System.Windows.Navigation;



namespace OnlineVideos.Views
{
    public partial class Settings : PhoneApplicationPage
    {
        #region GlobalDeclaration
        string myid = string.Empty;
        LoginPopup LoginPopup = new LoginPopup();
        CustomizationSettings objCustom = new CustomizationSettings();
        private SolidColorBrush adcontrolborder = new SolidColorBrush(Colors.Transparent);

        private static PageHelper oldstate;
        int PopupSetting, PopupSettings;
        #endregion

        #region Constructor
        public Settings()
        {
            InitializeComponent();
            LoadSettings();
            PopupSetting = 0;
            PopupSettings = 0;
            if (ResourceHelper.AppName != Apps.Indian_Cinema_Pro.ToString() && ResourceHelper.AppName != Apps.Kids_TV_Pro.ToString() && ResourceHelper.AppName != Apps.Story_Time_Pro.ToString())
            LoadAds();
        }


        #endregion

        #region "Common Methods"

        private void LoadAds()
        {
            try
            {
                PageHelper.LoadAdControl(LayoutRoot, adstackpl, 1);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadAds Method In SongDetails file", ex);
                string excepmess = "Exception in LoadAds Method In SongDetails file.\n\n" + ex.Message + "\n\n StackTrace :- \n" + ex.StackTrace;
            }

        }

        private void LoadDownLoadTheme()
        {
            if (AppSettings.AddNewShowIconVisibility)
            {
                toggleswitchRunUnderLoock.Visibility = Visibility.Visible;
            }
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (AppSettings.popupcount == true)
            {
                e.Cancel = true;

                LoginPopup.close();

                if (!SettingsHelper.Contains("Password"))
                {
                    toggleswitch.Content = "Off";
                    toggleswitch.IsChecked = false;
                    AppSettings.ParentalControl = false;
                }
            }
        }
        #endregion
         protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("Settings Page");
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom Method In settings.cs file", ex);
            }
        }
         protected override void OnNavigatedTo(NavigationEventArgs e)
         {
             try
             {
                 FlurryWP8SDK.Api.LogPageView();
             }
             catch (Exception ex)
             {

                 Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In settings file", ex);
             }
         }
        #region PageLoad
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            FlurryWP8SDK.Api.LogEvent("Settings Page", true);
            if (ResourceHelper.ProjectName == AppResources.VideoMix)
            {
                btnparentalcontrol.Visibility = Visibility.Collapsed;
                toggleswitch.Visibility = Visibility.Collapsed;
            }

         
            //if (ResourceHelper.ProjectName == "Story Time" || ResourceHelper.ProjectName == "Vedic Library")
            //{
            //    //sync.Visibility = Visibility.Visible;

            //}
            //else
            //{
            //    //sync.Visibility = Visibility.Collapsed;
            //}
            oldstate = PageHelper.InactivatePage(false);
            try
            {
                var settings = IsolatedStorageSettings.ApplicationSettings;
                if (SettingsHelper.Contains("UpdateMovies"))
                    toggleswitchUpdateMovies.IsChecked = AppSettings.AutomaticallyDownloadShows;

                if (settings.Contains("GeoLocationStatus"))
                    GpsLocationToggleSwitch.IsChecked = bool.Parse(settings["GeoLocationStatus"].ToString());
                if (settings.Contains("runUnderLock"))
                    toggleswitchRunUnderLoock.IsChecked = bool.Parse(settings["runUnderLock"].ToString());




                if (SettingsHelper.Contains("Parental Control"))
                {
                    if (AppSettings.ParentalControl)
                        PopupSetting = 1;
                    else
                        PopupSetting = 0;

                    if (NavigationContext.QueryString.TryGetValue("myid", out myid))
                    {
                        if (!AppSettings.ParentalControl)
                            PopupSetting = 0;
                        else
                            PopupSettings = 0;

                        toggleswitch.IsChecked = AppSettings.ParentalControl;
                    }
                    else
                    {
                        if (!AppSettings.ParentalControl)
                            PopupSetting = 1;
                        else
                            PopupSetting = 0;

                        if (AppSettings.ParentalControl)
                            PopupSettings = 1;
                        else
                            PopupSettings = 0;

                        toggleswitch.IsChecked = AppSettings.ParentalControl;
                    }
                }

                PageHelper.RemoveEntryFromBackStack("Settings");
                LoadDownLoadTheme();
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded Method In settings file", ex);
            }
        }

        private void LoadSettings()
        {
            if (!AppResources.ShowParentalControl)
            {
                btnparentalcontrol.Visibility = Visibility.Collapsed;
                toggleswitch.Visibility = Visibility.Collapsed;
            }
        }
        #endregion

        #region Events
        private void toggleswitchUpdateMovies_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                AppSettings.AutomaticallyDownloadShows = true;
                toggleswitchUpdateMovies.Content = "Data Plan";
                toggleswitchUpdateMovies.Foreground = new SolidColorBrush(Colors.White);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in toggleswitchUpdateMovies_Checked Method In Settings file", ex);
            }
        }
        private void toggleswitchUpdateMovies_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                AppSettings.AutomaticallyDownloadShows = false;
                toggleswitchUpdateMovies.Content = "Wi-Fi";
                toggleswitchUpdateMovies.Foreground = new SolidColorBrush(Colors.White);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in toggleswitchUpdateMovies_Unchecked Method In Settings file.", ex);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (AppSettings.ParentalControl)
            {
                LoginPopup.show("false");
                PageHelper.RemoveEntryFromBackStack("Settings");
            }
            else
                NavigationService.Navigate(NavigationHelper.ParentalControlShowListPage);
        }

        private void toggleswitch_Checked(object sender, RoutedEventArgs e)
        {
            if (PopupSettings != 1)
            {
                toggleswitch.Content = "On";

                if (!SettingsHelper.Contains("Password"))

                    LoginPopup.show("false");
                else
                    AppSettings.ParentalControl = true;

                PageHelper.RemoveEntryFromBackStack("Settings");

                PopupSettings = 0;
            }
            else
            {
                toggleswitch.Content = "On";
                PopupSettings = 0;
            }
        }
        private void toggleswitch_Unchecked(object sender, RoutedEventArgs e)
        {
            if (PopupSetting != 1)
            {
                if (SettingsHelper.Contains("Password"))
                {
                    LoginPopup.show("true");
                    PageHelper.RemoveEntryFromBackStack("Settings");
                    toggleswitch.Content = "Off";
                    PopupSetting = 0;
                }
                else
                {
                    toggleswitch.Content = "Off";
                    PopupSetting = 0;
                }
            }
            else
            {
                toggleswitch.Content = "Off";
                PopupSetting = 0;
            }

        }
        private void btbabout_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.AboutMemoryPage);
        }
        private void imgTitle_MouseEnter(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
        }
        #endregion

        private void GpsLocationToggleSwitch_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
              
                bool enabled = true;
                GeoCoordinateWatcher geo = new GeoCoordinateWatcher();
                geo.Start();
                if (geo.Permission == GeoPositionPermission.Denied)
                {
                    enabled = false;
                }
                geo.Stop();
                geo.Dispose();
                if (enabled)
                {
                GpsLocationToggleSwitch.Content = "On";
                SettingsHelper.Save("GeoLocationStatus", "true");
                }
                else
                {
                    MessageBoxResult result = MessageBox.Show("Your location services are turned off.To turn them on, go to location settings?", "Location services are off", MessageBoxButton.OK);
                  
                    GpsLocationToggleSwitch.IsChecked = false;

                }
            }
            catch (Exception ex)
            {
                string mess = "Exception in GpsLocationToggleSwitch_Checked Method In settings file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                ex.Data.Add("Date", DateTime.Now);
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void GpsLocationToggleSwitch_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {

                GpsLocationToggleSwitch.Content = "Off";
                SettingsHelper.Save("GeoLocationStatus", "false");
                toggleswitchRunUnderLoock.Content = "Off";
                SettingsHelper.Save("runUnderLock", "false");
                toggleswitchRunUnderLoock.IsChecked = false;
            }
            catch (Exception ex)
            {
                string mess = "Exception in GpsLocationToggleSwitch_Unchecked Method In settings file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                ex.Data.Add("Date", DateTime.Now);
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void lockToggleSwitch_Checked(object sender, RoutedEventArgs e)
        {


            bool IsLocationTrackingEnable = SettingsHelper.getBoolValue("GeoLocationStatus");

            if (IsLocationTrackingEnable)
            {
                if (!ApplicationIdleModeHelper.Current.RunsUnderLock)
                {
                    MessageBoxResult result = MessageBox.Show("Running under lockscreen can consume battery even when screen is locked and you are not using the application. Click OK to enable", "", MessageBoxButton.OKCancel);
                    if (result == MessageBoxResult.OK)
                    {
                        ApplicationIdleModeHelper.Current.HasUserAgreedToRunUnderLock = true;
                        ApplicationIdleModeHelper.Current.RunsUnderLock = true; 
                        toggleswitchRunUnderLoock.Content = "On";
                        SettingsHelper.Save("runUnderLock", "true");
                    }
                    else
                    {
                        ApplicationIdleModeHelper.Current.RunsUnderLock = false; 
                        toggleswitchRunUnderLoock.IsChecked = false;
                        SettingsHelper.Save("runUnderLock", "false");
                    }
                }
                else
                {
                    toggleswitchRunUnderLoock.Content = "On";
                    SettingsHelper.Save("runUnderLock", "true");
                }
            }
            else
            {
                MessageBox.Show("Location tracking must be enabled");
                toggleswitchRunUnderLoock.IsChecked = false;
            }
        }

        private void lockToggleSwitch_Unchecked(object sender, RoutedEventArgs e)
        {
            toggleswitchRunUnderLoock.Content = "Off";
            SettingsHelper.Save("runUnderLock", "false");
            ApplicationIdleModeHelper.Current.RunsUnderLock = false; 
        }



    }
}
